package com.lst.filter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.LoginHistroyMapper;
import com.lst.model.LoginHistroy;
import com.lst.utils.CommonUtils;
import com.lst.utils.TimerTaskTest;  
  
public class myFilter implements Filter{  
	
	  protected static ClassPathXmlApplicationContext ctx;
	  protected static String path;
	  
	  private LoginHistroyMapper loginHistroyMapper = ctx.getBean(LoginHistroyMapper.class);
	  
		/**
		 * @return the path
		 */
		public static String getPath() {
			return path;
		}

		/**
		 * @param path
		 *            the path to set
		 */
		public static void setPath(String path) {
			myFilter.path = path;
		}
		
		static {
			ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

			try {
				Properties props = new Properties();

				ClassLoader cl = CommonUtils.class.getClassLoader();

				InputStream is = cl.getResourceAsStream("kungfu.properties");

				props.load(is);

				path = props.getProperty("img_file_path_linux");
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		
		
  
    public void destroy() {  
          
    }  
  
    public void doFilter(ServletRequest arg0, ServletResponse arg1,  
            FilterChain arg2) throws IOException, ServletException {  
          
          
        HttpServletRequest request = (HttpServletRequest) arg0;  
        HttpServletResponse reponse = (HttpServletResponse) arg1;  
        
        BaseResponse resPara=new BaseResponse();
        
          System.out.println("拦截器执行");
        if(request.getParameter("token") != null && request.getParameter("token")!=""){  
        	System.out.println("拦截器执行。。。。");
        	Integer userid = Integer.valueOf(request.getParameter("userid"));  
        	 List<LoginHistroy> loginHistroyList=loginHistroyMapper.selectById(userid);
        	 if(loginHistroyList!=null){
        		 if(loginHistroyList.get(0).getReserved9()==request.getParameter("token")){
        			 arg2.doFilter(arg0, arg1);
        		 }else{
        			 resPara.setCode(CommCode.M_A000000); 
        			 resPara.setMessage("登陆失效，请重新登陆！");
        			 
        		 }
        	 }
        }  
        arg2.doFilter(arg0, arg1);  //告诉服务器这个过滤已经执行过了，可以继续向下执行了
    }  
  
    public void init(FilterConfig arg0) throws ServletException {  
          
    }  
  
}  